package com.weixin.paleituo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import com.weixin.paleituo.R;
import com.weixin.paleituo.activity.GoodsDetailsActivity;
import com.weixin.paleituo.activity.GoodsStoreActivity;
import com.weixin.paleituo.bean.CartBean;
import com.weixin.paleituo.bean.NofailureBean;
import com.weixin.paleituo.eventbus.ShopCartChildCheckEvent;
import com.weixin.paleituo.eventbus.ShopCartDeleteEvent;
import com.weixin.paleituo.eventbus.ShopCartGropCheckEvent;
import com.weixin.paleituo.eventbus.ShopCartNumberEvent;
import com.weixin.paleituo.util.LogUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.NumberButton;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Author yichao
 * Time  2018/3/29 14:33
 * Dest  购物车
 */
public class ShopCarAdapter extends BaseExpandableListAdapter {
    private String TAG="ShopCarAdapter";

    private Context mContext;
    private List<NofailureBean> mNofailureBeanList;

    public ShopCarAdapter(Context context, List<NofailureBean> nofailureBeanList) {
        mContext = context;
        mNofailureBeanList = nofailureBeanList;
    }
    @Override
    public int getGroupCount() {
        return mNofailureBeanList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        LogUtils.i(TAG,"mNofailureBeanList  length :"+mNofailureBeanList.size() +" position"+groupPosition);

        return mNofailureBeanList.get(groupPosition).getCart().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return mNofailureBeanList.get(groupPosition).getStoreId();
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return  mNofailureBeanList.get(groupPosition).getCart().get(childPosition).getCartId();
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }
    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHold groupHold;
        if (convertView==null){
            groupHold=new GroupHold();
            convertView=UIUtils.inflate(mContext, R.layout.layout_item_shop_car_group);
             groupHold.mCheckbox=   convertView.findViewById(R.id.cb_layout_item_shop_car_group);
            groupHold.llstoreName=    convertView.findViewById(R.id.ll_nameroot_layout_item_shop_car_group);
            groupHold.mstoreName=     convertView.findViewById(R.id.tv_storename_layout_item_shop_car_group);
            groupHold.mrlcbRoot=convertView.findViewById(R.id.rl_shop_store_cb_root_item_shop_car_group);
            convertView.setTag(groupHold);
        }else {
            groupHold= (GroupHold) convertView.getTag();
        }
    final     NofailureBean nofailureBean = mNofailureBeanList.get(groupPosition);
    if (nofailureBean.isCheck()){
        groupHold.mCheckbox.setBackgroundResource(R.mipmap.chooce_green);

    }else {
        groupHold.mCheckbox.setBackgroundResource(R.mipmap.xe_h);
    }
        groupHold.mstoreName.setText(nofailureBean.getStorename()+"");
        groupHold.llstoreName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  ToastUtils.makeText("去店铺");
                GoodsStoreActivity.startActivity(mContext,nofailureBean.getStoreId());
            }
        });

        groupHold.mrlcbRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (nofailureBean.isCheck()){
                    EventBus.getDefault().post(new ShopCartGropCheckEvent(groupPosition,false));
                }else {
                    EventBus.getDefault().post(new ShopCartGropCheckEvent(groupPosition,true));
                }
            }
        });



        return convertView;
    }
    @Override
    public View getChildView(final int groupPosition,final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      final   ChildHold childHold;
        if (convertView==null){
            convertView= UIUtils.inflate(mContext,R.layout.layout_item_shop_car_child);
            childHold=new ChildHold();
            childHold.mCheckbox=  convertView.findViewById(R.id.ck_layout_item_shop_car_child);
            childHold.mImageView=   convertView.findViewById(R.id.iv_icon_layout_item_shop_car_child);
            childHold.mgoodsName=   convertView.findViewById(R.id.tv_goodsname_layout_item_shop_car_child);
            childHold.mselectAttr=   convertView.findViewById(R.id.select_attr_layout_item_shop_car_child);
            childHold.mllspecicOffRoot=   convertView.findViewById(R.id.ll_speciall_offter_layout_item_shop_car_child);
            childHold.mspeciallPrice=   convertView.findViewById(R.id.tv_specical_offter_price_layout_item_shop_car_child);
            childHold.originalprice=  convertView.findViewById(R.id.tv_original_price_layout_item_shop_car_child);
            childHold.normalPrice= convertView.findViewById(R.id.tv_saler_nomal_price_layout_item_shop_car_child);
            childHold.normalSalePriceRoot=    convertView.findViewById(R.id.ll_noma_sale_price_root_layout_item_shop_car_child);
           childHold.mNumberButton= convertView.findViewById(R.id.number_button_layout_item_shop_car_child);
           childHold.mllitemRoot=convertView.findViewById(R.id.ll_root_layout_item_shop_car_child);
             childHold.mdelete=  convertView.findViewById(R.id.btnDelete);
             childHold.swipemenulayout=  convertView.findViewById(R.id.swipemenulayout_layout_my_message);
             childHold.mrlcbRoot=convertView.findViewById(R.id.rl_cb_root_layout_item_shop_car_child);
            convertView.setTag(childHold);
        }else {
            childHold= (ChildHold) convertView.getTag();
        }


  final       CartBean cartBean = mNofailureBeanList.get(groupPosition).getCart().get(childPosition);
        if (cartBean!=null ){
           if (cartBean.isIscheck()){
               childHold.mCheckbox.setBackgroundResource(R.mipmap.chooce_green);
           }else {
               childHold.mCheckbox.setBackgroundResource(R.mipmap.xe_h);
           }
            Glide.with(mContext).load(cartBean.getGoodsimage()).into(childHold.mImageView);
            childHold.mgoodsName.setText(cartBean.getGoodsname()+"");
            childHold.mselectAttr.setText(cartBean.getSpec_info()+"");
            if (cartBean.getDisprice()>0){
                childHold.mllspecicOffRoot.setVisibility(View.VISIBLE);
                childHold.normalSalePriceRoot.setVisibility(View.INVISIBLE);
                childHold.mspeciallPrice.setText("￥"+UIUtils.getMoney(cartBean.getDisprice()));
                childHold.originalprice.setText("￥"+UIUtils.getMoney(cartBean.getPrice()));
            }else {
                childHold.mllspecicOffRoot.setVisibility(View.INVISIBLE);
                childHold.normalSalePriceRoot.setVisibility(View.VISIBLE);
                childHold.normalPrice.setText("￥"+UIUtils.getMoney(cartBean.getPrice()));
            }

            childHold.mrlcbRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (cartBean.isIscheck()){
                        EventBus.getDefault().post(new ShopCartChildCheckEvent(groupPosition,childPosition,false));

                    }else {

                        EventBus.getDefault().post(new ShopCartChildCheckEvent(groupPosition,childPosition,true));
                    }
                }
            });

            childHold.mNumberButton.setBuyMax(200).setCurrentNumber(cartBean.getCount()).setInventory(200).setOnWarnListener(new NumberButton.OnWarnListener() {
                @Override
                public void onWarningForInventory(int inventory) {

                }

                @Override
                public void onWarningForBuyMax(int max) {

                }
                @Override
                public void number(int number) {
                  //  mNofailureBeanList.get(groupPosition).getCart().get(childPosition).setCount(number);

                    EventBus.getDefault().post(new ShopCartNumberEvent(groupPosition,childPosition,number));
                }
            });
            childHold.mllitemRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    GoodsDetailsActivity.startActivity(mContext,cartBean.getGoodsId());
                }
            });
        }
        childHold.mdelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new ShopCartDeleteEvent(groupPosition,childPosition,childHold));



            }
        });

        return convertView;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    class GroupHold{
        TextView mstoreName;
        CheckBox mCheckbox;
        LinearLayout llstoreName;
        RelativeLayout mrlcbRoot;

    }
   public class ChildHold{

        CheckBox mCheckbox;
        ImageView mImageView;
        TextView mgoodsName,mselectAttr,mspeciallPrice,originalprice,normalPrice;
        LinearLayout mllspecicOffRoot,normalSalePriceRoot,mllitemRoot;
        NumberButton mNumberButton;
        Button mdelete;
        RelativeLayout mrlcbRoot;
     public    SwipeMenuLayout swipemenulayout;


    }




}
